package com.cloud.mall.user.service.impl;

import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.ums.mapper.UmsPermissionMapper;
import com.cloud.mall.ums.model.UmsPermission;
import com.cloud.mall.ums.model.UmsPermissionExample;
import com.cloud.mall.user.dto.UmsPermissionNode;
import com.cloud.mall.user.input.UmsPermissionInpDTO;
import com.cloud.mall.user.output.UmsPermissionOut;
import com.cloud.mall.usr.service.UmsPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UmsPermissionServiceImpl implements UmsPermissionService {
    @Autowired
    private UmsPermissionMapper permissionMapper;

    @Override
    public BaseResponse create(@RequestBody UmsPermissionInpDTO umsPermissionDTO) {
        UmsPermission permission = MallBeanUtils.dtoToDo(umsPermissionDTO, UmsPermission.class);
        permission.setStatus(1);
        permission.setCreateTime(new Date());
        permission.setSort(0);
        int count= permissionMapper.insert(permission);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id,@RequestBody UmsPermissionInpDTO umsPermissionDTO) {
        UmsPermission permission = MallBeanUtils.dtoToDo(umsPermissionDTO,UmsPermission.class);
        permission.setId(id);
        int count= permissionMapper.updateByPrimaryKeySelective(permission);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse delete(List<Long> ids) {
        UmsPermissionExample example = new UmsPermissionExample();
        example.createCriteria().andIdIn(ids);
        int count=permissionMapper.deleteByExample(example);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse treeList() {
        List<UmsPermission> permissionList = permissionMapper.selectByExample(new UmsPermissionExample());
        List<UmsPermissionNode> result = permissionList.stream()
                .filter(permission -> permission.getPid().equals(0L))
                .map(permission -> covert(permission,permissionList)).collect(Collectors.toList());
        return BaseResponse.setResultSuccess(result);
    }

    @Override
    public BaseResponse<List<UmsPermissionOut>> list() {
        List<UmsPermission> permissionList=permissionMapper.selectByExample(new UmsPermissionExample());
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(permissionList,UmsPermissionOut.class));
    }

    /**
     * 将权限转换为带有子级的权限对象
     * 当找不到子级权限的时候map操作不会再递归调用covert
     */
    private UmsPermissionNode covert(UmsPermission permission,List<UmsPermission> permissionList){
        UmsPermissionNode node = new UmsPermissionNode();
        BeanUtils.copyProperties(permission,node);
        List<UmsPermissionNode> children = permissionList.stream()
                .filter(subPermission -> subPermission.getPid().equals(permission.getId()))
                .map(subPermission -> covert(subPermission,permissionList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }

}
